package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbContent;
import com.dongdong.pojo.TbContentCategory;
import com.dongdong.service.ContentCategoryService;
import com.dongdong.service.ContentService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {


    @Reference
    private ContentService contentService;

    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("Advertising")
    @ResponseBody
    public Result Advertising(){
        List<TbContent> contents = contentService.selectAll();
        return Result.builResult(ResultEnum.SUCCESS).setResout(contents);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContent tbContent){
        boolean b = contentService.insert(tbContent);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        boolean b = contentService.delete(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id, Model m){
        TbContent tbContent = contentService.selectId(id);
        m.addAttribute("t",tbContent.getStatus());
        m.addAttribute("ca",tbContent.getCategoryId());
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbContent);
    }


    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContent tbContent){
        boolean b = contentService.update(tbContent);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("confirm")
    @ResponseBody
    public Result confirm(TbContent tbContent){
        boolean b = contentService.updateSID(tbContent);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }
}
